package cnt5106.p2p.tracker;

import java.net.InetSocketAddress;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class DataManager {

	public void registerPeer(String filename, InetSocketAddress endpoint, int fileSize) {
		peerList.put(filename, new Vector<InetSocketAddress>());
		peerList.get(filename).add(endpoint);
		sizeList.put(filename, fileSize);
	}
	
	/**
	 * Return the peer list of current file. Doesn't include the file owner's endpoint and the peer's endpoint.
	 * @param filename the file's name
	 * @param endpoint the peer's socket address
	 * @return the peer list
	 */
	public void registerGroup(String filename, InetSocketAddress endpoint) {
		if (!peerList.get(filename).contains(endpoint)) {
			peerList.get(filename).add(endpoint);
		}
	}
	
	public Vector<String> list() {
		Vector<String> result = new Vector<String>();
		
		for (Enumeration<String> s = peerList.keys(); s.hasMoreElements();) {
			result.add(s.nextElement());
		}
		
		return result;
	}
	
	public boolean hasFile(String filename) {
		return peerList.containsKey(filename);
	}
	
	public Vector<InetSocketAddress> getPeerList(String filename) {
		return peerList.get(filename);
	}
	
	public int getFileSize(String filename) {
		return sizeList.get(filename);
	}

	private Hashtable<String, Vector<InetSocketAddress>> peerList = new Hashtable<String, Vector<InetSocketAddress>>();
	private Hashtable<String, Integer> sizeList = new Hashtable<String, Integer>();
}
